using System;
using System.IO;
using GalaSoft.MvvmLight;
using SfdClient.Resources;

namespace SfdClient.ViewModel
{
    public class FolderBrowserItem : ViewModelBase
    {
        public FolderBrowserItem(string rootFolder, string fullName)
        {
            if (string.IsNullOrEmpty(rootFolder))
                throw new ArgumentNullException("rootFolder");

            if ( !Directory.Exists(fullName) && !File.Exists(fullName))
                throw new ArgumentException(ResourceLocalizer.Current["FolderBrowserItem_DossierInconnu"], "fullName");

            RootFolder = Path.GetFullPath(rootFolder);
            FullName = Path.GetFullPath(fullName);
            IsDirectory = Directory.Exists(fullName);

            if (!IsDirectory)
            {
                LastModification = File.GetLastAccessTimeUtc(FullName);
                FriendlyLength = string.Format(ResourceLocalizer.Current["FolderBrowserView_Kb"], Math.Round(new FileInfo(FullName).Length / 1024f));
            }
            else
            {
                LastModification = Directory.GetLastWriteTimeUtc(FullName);
                FriendlyLength = string.Empty;
            }

        }

        #region IsDirectory Property

        /// <summary>
        /// The <see cref="IsDirectory" /> property's name.
        /// </summary>
        public const string IsDirectoryPropertyName = "IsDirectory";

        private bool _isDirectory;

        public bool IsDirectory
        {
            get { return this._isDirectory; }

            private set
            {
                if (this._isDirectory == value) return;

                this._isDirectory = value;
                RaisePropertyChanged(IsDirectoryPropertyName);
            }
        }

        #endregion

        #region RootFolder Property

        /// <summary>
        /// The <see cref="RootFolder" /> property's name.
        /// </summary>
        public const string RootFolderPropertyName = "RootFolder";

        private string _rootFolder;

        public string RootFolder
        {
            get { return this._rootFolder; }

            private set
            {
                if (this._rootFolder == value) return;

                this._rootFolder = value;
                RaisePropertyChanged(RootFolderPropertyName);
            }
        }

        #endregion

        #region FullName Property

        /// <summary>
        /// The <see cref="FullName" /> property's name.
        /// </summary>
        public const string FullNamePropertyName = "FullName";

        private string _fullName;

        public string FullName
        {
            get { return _fullName; }

            private set
            {
                if (_fullName == value) return;

                _fullName = value;
                RaisePropertyChanged(FullNamePropertyName);
                RaisePropertyChanged(FriendlyNamePropertyName);
            }
        }

        #endregion

        #region FriendlyName Property

        /// <summary>
        /// The <see cref="FriendlyName" /> property's name.
        /// </summary>
        public const string FriendlyNamePropertyName = "FriendlyName";

 
        public string FriendlyName
        {
            get { return Path.GetFileName(FullName); }


        }

        #endregion

        #region LastModification Property

        /// <summary>
        /// The <see cref="LastModification" /> property's name.
        /// </summary>
        public const string LastModificationPropertyName = "LastModification";

        private DateTime _lastModification;

        public DateTime LastModification
        {
            get { return this._lastModification; }

            private set
            {
                if (this._lastModification == value) return;

                this._lastModification = value;
                RaisePropertyChanged(LastModificationPropertyName);
            }
        }

        #endregion

        #region FriendlyLength Property

        /// <summary>
        /// The <see cref="FriendlyLength" /> property's name.
        /// </summary>
        public const string FriendlyLengthPropertyName = "FriendlyLength";

        private string _friendlyLength;

        public string FriendlyLength
        {
            get { return _friendlyLength; }

            private set
            {
                if (_friendlyLength == value) return;

                _friendlyLength = value;
                RaisePropertyChanged(FriendlyLengthPropertyName);
            }
        }

        #endregion  
    }
}